package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.response.ActiveUser;
import com.goods.common.vo.business.HealthVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/10 15:28
 */
@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;

   // 是否打卡
    @Override
    public HealthVO isReport() {
        //根据userId今日日期查是否在有

        //获取userName
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long id=activeUser.getUser().getId();
        //今日日期零点,,2021-11-10 00:00:00
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY,0);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date toDayTime = instance.getTime();
        //构造查询条件
        Example example = new Example(Health.class);
        example.createCriteria().andEqualTo("userId",id)
                .andGreaterThan("createTime",toDayTime);
        Health health = healthMapper.selectOneByExample(example);
        if(null == health){
            return null;
        }
        HealthVO healthVO = new HealthVO();
        BeanUtils.copyProperties(health,healthVO);
        return healthVO;
    }

    //打卡
    @Override
    public void report(HealthVO healthVo) {
        healthVo.setCreateTime(new Date());
        //获取用户id
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long userId=activeUser.getUser().getId();
        healthVo.setUserId(userId);
        Health health = new Health();
        BeanUtils.copyProperties(healthVo, health);
        healthMapper.insertSelective(health);
    }

    //签到记录
    @Override
    public Map history(Map paramMap) {
        String pageNum = (String) paramMap.get("pageNum");
        String pageSize = (String) paramMap.get("pageSize");

        //获取用户id
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long userId=activeUser.getUser().getId();

        Example example = new Example(Health.class);
        example.createCriteria().andEqualTo("userId",userId);
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<Health> healthList = healthMapper.selectByExample(example);
        PageInfo<Health> pageInfo = new PageInfo<>(healthList);
        Map result= new HashMap<>();
        result.put("rows",pageInfo.getList());
        result.put("total",pageInfo.getTotal());
        return result;
    }
}
